package com.winit.datasync.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.winit.datasync.dao.UpsCustomerMerchSaleDao;
import com.winit.datasync.entity.UpsCustomerMerchSale;
import com.winit.datasync.utils.ConstantUtils;
import com.winit.datasync.utils.DateUtil;

/**
 * upsCustomerMerchSaleDao 数据层 ups商品信息表
 * 
 * @author liangming.deng
 *
 */
@Repository("upsCustomerMerchSaleDao")
public class UpsCustomerMerchSaleDaoImpl implements UpsCustomerMerchSaleDao {

	@Autowired
	private CassandraTemplate cassandraTemplate;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void batchInsert(final List<UpsCustomerMerchSale> list, int dataSourceType) {
		if (list == null || list.size() == 0)
			return;
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
			jdbcTemplate.batchUpdate(ConstantUtils.ORACLE_BATCH_INSERT_UPS_CUSTOMER_MERCH_SALE,
					new BatchPreparedStatementSetter() {

						@Override
						public void setValues(PreparedStatement ps, int index) throws SQLException {
							UpsCustomerMerchSale upsCustomerMerchSale = list.get(index);

							ps.setString(1, upsCustomerMerchSale.getWarehouse_code());
							ps.setString(2, upsCustomerMerchSale.getWarehouse_name());
							ps.setString(3, upsCustomerMerchSale.getCustomer_code());
							ps.setString(4, upsCustomerMerchSale.getCustomer_name());
							ps.setString(5, upsCustomerMerchSale.getMerchandise_code());
							ps.setString(6, upsCustomerMerchSale.getMerchandise_serno());
							ps.setString(7, upsCustomerMerchSale.getQty());
							ps.setString(8, upsCustomerMerchSale.getRemarks());
							ps.setTimestamp(9, DateUtil.convertUtilDateToSqlTimestamp(
									upsCustomerMerchSale.getCreated()));
							ps.setString(10, upsCustomerMerchSale.getCreatedby());
							ps.setString(11, upsCustomerMerchSale.getIsactive());
							ps.setString(12, upsCustomerMerchSale.getIsdelete());
							ps.setTimestamp(13, DateUtil.convertUtilDateToSqlTimestamp(
									upsCustomerMerchSale.getUpdated()));
							ps.setString(14, upsCustomerMerchSale.getUpdatedby());
						}

						@Override
						public int getBatchSize() {
							return list.size();
						}
					});
		} else {
			mongoTemplate.insert(list, UpsCustomerMerchSale.class);
		}
	}

	@Override
	public void delete(UpsCustomerMerchSale upsCustomerMerchSale, int dataSourceType) {
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
			jdbcTemplate.execute(ConstantUtils.ORACLE_DELETE_UPS_CUSTOMER_MERCH_SALE);
		} else {
			Query query = new Query();
			query.addCriteria(Criteria.where("_id").is(upsCustomerMerchSale.getId()));

			mongoTemplate.remove(query, UpsCustomerMerchSale.class);
		}
	}

	@Override
	public List<UpsCustomerMerchSale> getByParams(HashMap<String, String> params,
			int dataSourceType) {

		StringBuilder sBuilder = new StringBuilder(
				ConstantUtils.ORACLE_GET_UPS_CUSTOMER_MERCH_SALE);
		if (params != null && !params.isEmpty()) {
			sBuilder.append(ConstantUtils.WHERE);

			// 拼接
			for (String key : params.keySet()) {
				sBuilder.append(StringUtils.EMPTY).append(key).append(ConstantUtils.EQUAL)
						.append(params.get(key));
			}

		}

		if (3 == dataSourceType) {
			return cassandraTemplate.select(sBuilder.toString(), UpsCustomerMerchSale.class);
		} else if (1 == dataSourceType) {
			return null;
		} else {
			return null;
		}

	}

	@Override
	public List<UpsCustomerMerchSale> getAll(int dataSourceType) {
		if (3 == dataSourceType) {
			return cassandraTemplate.select(ConstantUtils.CASSANDRA_GET_UPS_CUSTOMER_MERCH_SALE,
					UpsCustomerMerchSale.class);
		} else if (1 == dataSourceType) {
			return jdbcTemplate.query(ConstantUtils.ORACLE_GET_UPS_CUSTOMER_MERCH_SALE,
					new RowMapper<UpsCustomerMerchSale>() {

						@Override
						public UpsCustomerMerchSale mapRow(ResultSet rs, int rowNum)
								throws SQLException {

							UpsCustomerMerchSale upsCustomerMerchSale = new UpsCustomerMerchSale();

							upsCustomerMerchSale.setCreated(DateUtil
									.convertSqlTimestampToUtilDate(rs.getTimestamp("created")));
							upsCustomerMerchSale.setCreatedby(rs.getString("createdby"));
							upsCustomerMerchSale.setCustomer_code(rs.getString("customer_code"));
							upsCustomerMerchSale.setCustomer_name(rs.getString("customer_name"));
							upsCustomerMerchSale.setIsactive(rs.getString("isactive"));
							upsCustomerMerchSale.setIsdelete(rs.getString("isdelete"));
							upsCustomerMerchSale
									.setMerchandise_code(rs.getString("merchandise_code"));
							upsCustomerMerchSale
									.setMerchandise_serno(rs.getString("merchandise_serno"));
							upsCustomerMerchSale.setQty(rs.getString("qty"));
							upsCustomerMerchSale.setRemarks(rs.getString("remarks"));
							upsCustomerMerchSale.setUpdated(DateUtil
									.convertSqlTimestampToUtilDate(rs.getTimestamp("updated")));
							upsCustomerMerchSale.setUpdatedby(rs.getString("updatedby"));
							upsCustomerMerchSale.setWarehouse_code(rs.getString("warehouse_code"));
							upsCustomerMerchSale.setWarehouse_name(rs.getString("warehouse_name"));

							return upsCustomerMerchSale;
						}
					});
		} else {
			return mongoTemplate.findAll(UpsCustomerMerchSale.class);
		}

	}

}
